package cn.findviewbydream.vant.plugin;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.intellij.openapi.util.io.StreamUtil;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * cn.findviewbydream.vant.plugin
 *
 * @author 白雨浓
 * date: 2019/11/12 15:06
 * content:
 */
public class VantDoc {

    private static Map<String, Item> cache = null;

    private static VantDoc vantDoc = null;

    public static VantDoc newInstance() {
        if (vantDoc != null) {
            return vantDoc;
        }

        vantDoc = new VantDoc();
        return vantDoc;
    }

    public Item getItem(String name) {

        if (cache != null) {
            return cache.get(name);
        }

        String content = readTemplateFile("/json/vant-doc.json");

        Type type = new TypeToken<Map<String, Item>>() {
        }.getType();
        Map<String, Item> map = new Gson().fromJson(content, type);

        cache = new ConcurrentHashMap<>(map);

        return cache.get(name);
    }

    private String readTemplateFile(String fileName) {
        InputStream in;
        String content = "";
        try {
            in = VantDoc.class.getClassLoader().getResource(fileName).openStream();
            content = new String(StreamUtil.loadFromStream(in));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content;
    }

    public Map<String, Item> getMap() {
        return cache;
    }

    public class Item {

        private List<Prop> props;

        private List<Method> methods;

        public List<Prop> getProps() {
            return props;
        }

        public void setProps(List<Prop> props) {
            this.props = props;
        }

        public List<Method> getMethods() {
            return methods;
        }

        public void setMethods(List<Method> methods) {
            this.methods = methods;
        }
    }


    public class Prop {

        private String prop;

        private String desc;

        private List<String> types;

        private String defaultValue;

        private String version;

        public String getProp() {
            return prop;
        }

        public void setProp(String prop) {
            this.prop = prop;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }

        public List<String> getTypes() {
            return types;
        }

        public void setTypes(List<String> types) {
            this.types = types;
        }

        public String getDefaultValue() {
            return defaultValue;
        }

        public void setDefaultValue(String defaultValue) {
            this.defaultValue = defaultValue;
        }

        public String getVersion() {
            return version;
        }

        public void setVersion(String version) {
            this.version = version;
        }
    }


    public class Method {

        private String method;

        private String desc;

        private String param;

        public String getMethod() {
            return method;
        }

        public void setMethod(String method) {
            this.method = method;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }

        public String getParam() {
            return param;
        }

        public void setParam(String param) {
            this.param = param;
        }
    }
}
